<template>
  <div class="cart">
    <h2>购物车</h2>
    <div v-if="cartItems.length === 0">购物车为空</div>
    <div v-else>
      <div v-for="item in cartItems" :key="item.id" class="cart-item">
        <span>{{ item.name }} - ¥{{ item.price }} - 数量：{{ item.quantity }}</span>
        <button @click="increment(item.id)">+</button>
        <button @click="decrement(item.id)">-</button>
        <button @click="removeItem(item.id)">删除</button>
      </div>
      <div class="total">总价：¥{{ cartTotal }}</div>
    </div>
  </div>
</template>

<script>
import { mapGetters, mapActions } from 'vuex'

export default {
  computed: {
    ...mapGetters(['cartItems', 'cartTotal'])
  },
  methods: {
    ...mapActions(['removeItem', 'incrementQuantity', 'decrementQuantity']),
    
    increment(id) {
      this.incrementQuantity(id)
    },
    
    decrement(id) {
      this.decrementQuantity(id)
    }
  }
}
</script>

<style>
.cart-item {
  margin: 10px 0;
  padding: 10px;
  border: 1px solid #eee;
}

button {
  margin-left: 5px;
}

.total {
  font-weight: bold;
  margin-top: 20px;
}
</style>